package com.xh.bussiness.xkw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xh.bussiness.tiku.model.Knowledge;
import com.xh.bussiness.tiku.service.IKnowledgeService;
import com.xh.bussiness.xkw.constant.XkwAPIURL;
import com.xh.bussiness.xkw.dao.XkwCategoryMapper;
import com.xh.bussiness.xkw.model.XkwCategory;
import com.xh.bussiness.xkw.service.IXkwCategoryService;
import com.xh.bussiness.xkw.utils.ConfigUtils;
import com.xh.bussiness.xkw.utils.XkwHttpsUtils;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class XkwCategoryServiceImpl extends BaseServiceImpl<XkwCategoryMapper, XkwCategory> implements IXkwCategoryService {


    private final String XkwApiSeriveUrl;

    @Autowired
    public XkwCategoryServiceImpl(ConfigUtils configUtils) {
        XkwApiSeriveUrl = configUtils.getXkwApiSeriveUrl();
    }
    @Autowired
    private IKnowledgeService knowledgeService;

    /**
     *  保存所有学科网子知识点
     * @throws Exception
     */
    public  void  SysXkwCategories() throws Exception {
        List<XkwCategory> xkwRootCategories = queryXkwRootCategories();
        if (!xkwRootCategories.isEmpty()) {
            xkwRootCategories.forEach(xkwCategory -> {
                Integer xkwId = xkwCategory.getXkwId();
                Integer subjectId = xkwCategory.getXkwSubjectId();
                try {
                    List<XkwCategory> xkwChildCategories = queryXkwChildCategories(xkwId, subjectId);
                    if (!xkwChildCategories.isEmpty()) {
                        for (XkwCategory xkwChildCategory : xkwChildCategories) {
                            Knowledge knowledge = new Knowledge();
                            knowledge.setId(String.valueOf(xkwChildCategory.getXkwId()));
                            knowledge.setName(xkwChildCategory.getName());
                            knowledge.setSubject(String.valueOf(subjectId));
                            knowledge.setParentId(String.valueOf(xkwChildCategory.getParentId()));
                            knowledge.setPath(xkwChildCategory.getPath());
                            knowledge.setPathName(xkwChildCategory.getPathName());
                            knowledge.setParentPath(xkwChildCategory.getParentPathId());
                            knowledge.setParentPathName(xkwChildCategory.getParentPathName());
                            knowledgeService.insert(knowledge);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }
    /**
     *  查询所有学科网根知识点
     * @throws Exception
     */
    public List<XkwCategory> queryXkwRootCategories() throws Exception {
        List<XkwCategory> xkwCategoriesList = new ArrayList<>();
        String url = XkwApiSeriveUrl + XkwAPIURL.GET_BANKS_CATEGORY_ROOT;
        String categoriesJson = XkwHttpsUtils.doGet(url);
        List<Map> parse = (List<Map>) JSONArray.parse(categoriesJson);
        parse.forEach(map -> {
            Integer id = Integer.valueOf(map.get("id").toString());
            String name = map.get("name").toString();
            Integer bankId = Integer.valueOf(map.get("bankId").toString());
            XkwCategory xkwCategory = new XkwCategory(id,bankId,name,0,1);
            xkwCategoriesList.add(xkwCategory);
        });
        return xkwCategoriesList;
    }

    /**
     *  查询所有学科网子知识点
     * @param rootId  根节点Id
     * @throws Exception
     */
    public  List<XkwCategory>  queryXkwChildCategories(Integer rootId, Integer subjectId) throws Exception {
        List<XkwCategory> xkwChildList = new ArrayList<>();
        String queryUrl = String.format(XkwAPIURL.GET_CATEGORIES_BY_ID,rootId);
        String url = XkwApiSeriveUrl + queryUrl;
        String childCategoriesJson = XkwHttpsUtils.doGet(url);

        Map childMap = JSONObject.parseObject(childCategoriesJson, Map.class);
        Integer id = Integer.valueOf(childMap.get("id").toString());
        String name = childMap.get("name").toString();
        Integer show = Integer.valueOf(childMap.get("show").toString());
        if(show == 1){
            String childNodeJson = childMap.get("categories").toString();

            XkwCategory xkwCategory = new XkwCategory(id,subjectId, name, 0, show);
            xkwChildList.add(xkwCategory);

            getChildCategories(id, subjectId, xkwChildList, childNodeJson,"#"+id+"#","#"+name+"#");
        }

        return xkwChildList;
    }


    /**
     *  递归知识点子节点
     * @param rootId  父ID
     * @param xkwCategoriesList   存储集合
     * @param childCategoriesJson 子节点Json
     */
    private void getChildCategories(Integer rootId,Integer subjectId, List<XkwCategory> xkwCategoriesList, String childCategoriesJson, String parentPathId, String parentPathName) {
        List<Map> parse = (List<Map>) JSONArray.parse(childCategoriesJson);
        if(!parse.isEmpty()){
            parse.forEach(map -> {
                Integer id = Integer.valueOf(map.get("id").toString());
                String name = map.get("name").toString();
                Integer show = Integer.valueOf(map.get("show").toString());
                if(show == 1){
                    XkwCategory xkwCategory = new XkwCategory(id,subjectId, name, rootId, show);
                    xkwCategory.setParentPathId(parentPathId);
                    xkwCategory.setParentPathName(parentPathName);
                    xkwCategory.setPath(xkwCategory.getParentPathId()+id+"#");
                    xkwCategory.setPathName(xkwCategory.getParentPathName()+name+"#");
                    xkwCategoriesList.add(xkwCategory);
                    String childNodeJson = map.get("categories").toString();
                    getChildCategories(id,subjectId,xkwCategoriesList,childNodeJson,xkwCategory.getPath(), xkwCategory.getPathName());
                }
            });
        }
    }


}
